﻿namespace _01_Tower
{
    
    internal class Map
    {
        string fileName;
        public int[,] map;

        public void Init()
        {
            
            fileName = "../../../map.csv";
            map = new int[30, 60];
            if (File.Exists(fileName))
            {
                // 读取文件, 这里using表示只在这里读取(使用？)，出了之后释放？以免内存泄漏
                using (StreamReader sr = new StreamReader(fileName))
                {
                    string data = sr.ReadToEnd();
                    // 字符串分割 Split, 这里的文本中用",""换行"分离
                    string[] lines = data.Split('\n'); // 分隔的也可使字符串
                    for (int i = 0; i < lines.Length; i++)
                    {
                        string[] elements = lines[i].Split(',');
                        for (int j = 0; j < elements.Length; j++)
                        {
                            map[i, j] = int.Parse(elements[j]);
                            if (map[i, j] == (int)MapType.Spawn)
                            {
                                map[i, j] = (int)MapType.None;
                                Hero.pos.x = j;
                                Hero.pos.y = i;
                            }
                        }
                    }
                };
            }
        }
        public void DrawMap(int[,] data)
        {
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    // 根据地图数据 绘制相应符号
                    switch (data[i, j])
                    {
                        case (int)MapType.Wall:
                            // 墙
                            Console.BackgroundColor = ConsoleColor.Gray;
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.SetCursorPosition(j, i); Console.Write("X");
                            Console.ResetColor();
                            break;
                        case (int)MapType.None:
                            // 空气
                            Console.SetCursorPosition(j, i); Console.Write(" ");
                            break;
                        case (int)MapType.Mon:
                            // 怪物
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.SetCursorPosition(j, i); Console.Write("a");
                            Console.ResetColor();
                            break;
                        case (int)MapType.Tool:
                            // 道具
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.SetCursorPosition(j, i); Console.Write("$");
                            Console.ResetColor();
                            break;
                        case (int)MapType.Step:
                            // 楼梯
                            Console.SetCursorPosition(j, i); Console.Write("L");
                            break;
                        case (int)MapType.Spawn:
                            // 出生点
                            Console.SetCursorPosition(j, i); Console.Write("^");
                            break;
                        case (int)MapType.MonSlime:
                            // 出生点
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.SetCursorPosition(j, i); Console.Write("S");
                            break;
                        case (int)MapType.MonGoblin:
                            // 出生点
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.SetCursorPosition(j, i); Console.Write("G");
                            break;
                    }
                }
            }
        }

        
        public void Input()
        {

        }
        public void Update()
        {

        }
        public void Render()
        {
           
            DrawMap(map);
        }
        public enum MapType
        {
            None = 1,
            Wall = 0,
            Mon = 2,
            Tool = 3,
            Step = 4,
            Spawn = 6,
            MonSlime = 7,
            MonGoblin= 8
        };

    }

}

